<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<h3>使用prototype继承</h3>
		<ul>
			<li>通过将子类的prototype设置成为基类prototype的一份拷贝，可使子类获得基类的所有prototype属性</li>
			<li>如果要在子类函数中调用基类的方法，可使用call来进行</li>
			<button onclick="funcTest1()">运行例子</button>
		</ul>
		<script>
			function Person(name, gender){
				this.name = name;
				this.gender = gender;
				Person.prototype.toString = function(){
					return "name:" + this.name + ",gender:" + this.gender;
				}
			}
			function Employee(name, gender, email){
				Person.call(this, name, gender);	// 调用基类构造函数
				this.email = email;
				Employee.prototype.toString = function(){
					var s1 = Person.prototype.toString.call(this);	// 调用基类对应函数
					var s2 = ",email=" + this.email;			
					return s1 + s2;
				};
				// 从Person复制了一份prototype赋给Employee，从而后者可以使用Person类的所有方法(继承)
				Employee.prototype = new Person();
			}
			 
			// 下列代码如果写在Employee函数外部，则后一句会把前一句覆盖
//			Employee.prototype.toString = function(){
//				var s1 = Person.prototype.toString.call(this);	// 调用基类对应函数
//				var s2 = ",email=" + this.email;			
//				return s1 + s2;
//			};
//			Employee.prototype = new Person();
			
			function funcTest1(){
				var emp1 = new Employee("Jerry", "男", "jerry@chinasofti.com");
				var emp2 = new Employee("Jam", "女", "jam@chinasofti.com");
				console.log(emp1.toString());
				console.log(emp2.toString());
				
				// 虽然Employee的prototype来源于Person的prototype，但二者并不是同一个内存
				console.log(Employee.prototype == Person.prototype);
			}
		</script>
		
	</body>
</html>
